//
//  GoodsViewController.m
//  ai2ban
//
//  Created by Heartbeats on 2018/2/26.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "GoodsViewController.h"
#import "ProductCell.h"
#import "PruoductViewController.h"

@interface GoodsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *products;
@end

@implementation GoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商店";
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ProductCell" bundle:nil] forCellReuseIdentifier:@"ProductCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
    
    [self loadData];
}

- (void) loadData {
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/getRecomelistHead" parameters:nil method:@"GET" callBack:^(id responseObject) {
        [ActivityIndicatorView dismiss];
        if ([responseObject[@"state"] intValue]) {
            self.products = responseObject[@"data"];
            [self.tableView reloadData];
            return;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.products.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.productDict = self.products[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PruoductViewController *shopView = [[PruoductViewController alloc] init];
    shopView.product = self.products[indexPath.row];
    [self.navigationController pushViewController:shopView animated:true];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

@end
